package view{
	import assets.Intro;
	import assets.Piramide;
	import assets.User;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	
	import model.AppModel;
	
	public class View extends Sprite{
		// Properties
		private var appModel:AppModel;
		
		private var intro:Intro;
		private var userContainer:Sprite;
		private var user:User;
		private var piramide:Piramide;
		private var yPos:uint;
		
		private var warningBg:Sprite;
		private var warning:Warning;
		
		// Constructor
		public function View(){
			trace("constructor [view]");
			this.appModel = AppModel.getInstance();
			
			appModel.addEventListener(AppModel.DELETE_INTRO, deleteIntro);
			
			intro = new Intro();
			addChild(intro);
		}
		
		// Methodes
		private function makeUsers():void{
			//useraanmaken en toevoegen in de container via for each van userArr in appmodel
			userContainer = new Sprite();
			for each(var name:String in appModel.namesArr){
				user = new User(name);
				user.scaleX = user.scaleY = 0.8;
				user.y = yPos;
				userContainer.addChild(user);
				yPos += user.height + 3;
			}
			userContainer.x = piramide.width +5;
			addChild(userContainer);
		}
		
		private function makePiramide():void{
			piramide = new Piramide();
			addChild(piramide);
		}
		
		private function toggleWarning(warningsString:String = ""):void{
			warningBg = new Sprite();
			warning = new Warning();
			
			warningBg.graphics.beginFill(0x5E5E5E, 0);
			warningBg.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			warningBg.graphics.endFill();
			
			addChild(warningBg);
			TweenLite.to(warningBg, .4, {alpha:.6});
			
			// BlurFilter
			var blur:BlurFilter=new BlurFilter(30,30,1);
			warningBg.filters=[blur];
			
			addChild(warning);
			warning.x = stage.stageWidth*.5 - warning.width*.5;
			warning.y = stage.stageHeight*.5 - warning.height*.5;
			
			warning.txt.text = warningsString;
			
			warning.addEventListener(MouseEvent.CLICK, warningClickHandler);
		}
		
		private function removeWarning():void{
			removeChild(warningBg);
			removeChild(warning);
		}
		
		// Handlers
		private function deleteIntro(e:Event):void{
			removeChild(intro);
			makePiramide();
			makeUsers();
		}
		
		private function warningClickHandler(e:Event):void{
			this.removeEventListener(MouseEvent.CLICK, warningClickHandler);
			TweenLite.to(warning, .4, {alpha:0});
			TweenLite.to(warningBg, .4, {alpha:0, onComplete:removeWarning} );
		}
	}
}